﻿using System;
using Kiva7.Utilities;
using Kiva7.DataResponse;
using Kiva7.DataClasses;
using Kiva7.Baskets;

namespace Kiva7.DataRequests
{
    public class LoanSearchRequest : DataRequest<LoansResponse, Loan>
    {
        public LoanSearchRequest()
            : base(true, false, false)
        {
        }


        public Uri BuildUri(string query,
            string status,
            string gender,
            string region,
            string countryCode,
            string sector,
            string partnerId,
            string sortBy,
            string hasCurrencyLoss)
        {
            UrlCreator url = new UrlCreator(BaseUrl + "loans/search.xml");

            url.Add("q", query);
            url.Add("status", status);
            url.Add("gender", gender);
            url.Add("region", region);
            url.Add("country_code", countryCode);
            url.Add("sector", sector);
            url.Add("partner", partnerId);
            url.Add("sort_by", sortBy);

            url.Add("app_id", KivaAppId);

            return url.ToUri();
        }


        internal void GetLoans(string query,
            string status,
            string gender,
            string region,
            string countryCode,
            string sector,
            string partnerId,
            string sortBy,
            string hasCurrencyLoss)
        {
            LoadResponse(BuildUri(query,
                                    status,
                                    gender,
                                    region,
                                    countryCode,
                                    sector,
                                    partnerId,
                                    sortBy,
                                    hasCurrencyLoss));
        }

        protected override void CopyResponse(LoansResponse response)
        {
            base.CopyResponse(response);
            foreach (var data in Data)
            {
                data.IsSelected = Basket.Current.Contains(data.Id);
            }
        }
    }

}
